//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.biz.IManagerBiz;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.constant.e.ManagerAdminEnum;
import net.mingsoft.basic.entity.AppEntity;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-基础接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/basic/manager"})
public class ManagerAction extends BaseAction {
    @Autowired
    private IManagerBiz managerBiz;

    public ManagerAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    @RequiresPermissions({"basic:manager:view"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/WEB-INF/manager/basic/manager/index";
    }

    @ApiOperation("查询管理员列表")
    @GetMapping({"/list"})
    @RequiresPermissions({"basic:manager:view"})
    @ResponseBody
    public ResultData list(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        AppEntity websiteApp = BasicUtil.getWebsiteApp();
        List managerList;
        if (websiteApp != null) {
            String appId = websiteApp.getAppId();
            LambdaQueryWrapper<ManagerEntity> wrapper = new LambdaQueryWrapper();
            wrapper.like(StringUtils.isNotBlank(manager.getManagerName()), ManagerEntity::getManagerName, manager.getManagerName());
            wrapper.like(StringUtils.isNotBlank(manager.getManagerNickName()), ManagerEntity::getManagerNickName, manager.getManagerNickName());
            wrapper.eq(StringUtils.isNotBlank(manager.getRoleIds()), ManagerEntity::getRoleIds, manager.getRoleIds());
            wrapper.apply("APP_ID={0}", new Object[]{appId});
            managerList = this.managerBiz.list(wrapper);
        } else {
            managerList = this.managerBiz.query(manager);
        }

        List<ManagerEntity> allManager = this.managerBiz.queryAllManager(managerList);
        return ResultData.build().success(BasicUtil.filter(new EUListBean(allManager, (int)BasicUtil.endPage(allManager).getTotal()), new String[]{"managerPassword", "updateBy", "createBy", "del"}));
    }

    @ApiOperation("查询管理员列表,去掉当前管理员id，确保不能删除和修改自己")
    @GetMapping({"/query"})
    @RequiresPermissions({"basic:manager:view"})
    @ResponseBody
    public ResultData query(HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        ManagerEntity manager = BasicUtil.getManager();
        BasicUtil.startPage();
        AppEntity websiteApp = BasicUtil.getWebsiteApp();
        List managerList;
        if (websiteApp != null) {
            String appId = websiteApp.getAppId();
            QueryWrapper<ManagerEntity> wrapper = (QueryWrapper)(new QueryWrapper()).eq("APP_ID", appId);
            managerList = this.managerBiz.list(wrapper);
        } else {
            managerList = this.managerBiz.list();
        }

        List<ManagerEntity> allManager = this.managerBiz.queryAllManager(managerList);
        Iterator var11 = allManager.iterator();

        while(var11.hasNext()) {
            ManagerEntity _manager = (ManagerEntity)var11.next();

            assert manager != null;

            if (_manager.getId().equals(manager.getId())) {
                _manager.setId("0");
            }
        }

        return ResultData.build().success(new EUListBean(allManager, (int)BasicUtil.endPage(allManager).getTotal()));
    }

    @ApiOperation("获取管理员接口")
    @GetMapping({"/get"})
    @RequiresPermissions({"basic:manager:view"})
    @ResponseBody
    public ResultData get(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        ManagerEntity managerEntity;
        if (StringUtils.isNotEmpty(manager.getId())) {
            managerEntity = (ManagerEntity)this.managerBiz.getById(manager.getId());
        } else {
            ManagerEntity managerSession = BasicUtil.getManager();
            if (managerSession == null) {
                return ResultData.build().error("管理员已失效!");
            }

            managerEntity = (ManagerEntity)this.managerBiz.getById(managerSession.getId());
        }

        if (managerEntity != null) {
            managerEntity.setManagerPassword("");
        }

        return ResultData.build().success(managerEntity);
    }

    @ApiOperation("获取当前管理员信息接口")
    @GetMapping({"/info"})
    @ResponseBody
    public ResultData info(HttpServletResponse response, HttpServletRequest request) {
        ManagerEntity manager = BasicUtil.getManager();
        if (manager == null) {
            return ResultData.build().error("管理员已失效!");
        } else {
            ManagerEntity managerEntity = (ManagerEntity)this.managerBiz.getById(manager.getId());
            if (managerEntity != null) {
                managerEntity.setManagerPassword("");
            }

            return ResultData.build().success(managerEntity);
        }
    }

    @ApiOperation("保存管理员实体")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "managerName",
            value = "帐号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerNickName",
            value = "昵称",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerPassword",
            value = "密码",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "roleId",
            value = "角色ID",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "peopleId",
            value = "用户ID",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "保存管理员实体",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/save"})
    @ResponseBody
    @RequiresPermissions({"basic:manager:save"})
    public ResultData save(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isBlank(manager.getManagerName())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("manager.name")}));
        } else if (!StringUtil.checkLength(manager.getManagerName() + "", 6, 15)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("manager.name"), "6", "15"}));
        } else if (this.managerBiz.getManagerByManagerName(manager.getManagerName()) != null) {
            return ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("manager.name")}));
        } else if (!manager.getManagerName().matches("^[a-zA-Z0-9_]{6,15}$")) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager.name")}));
        } else if (StringUtils.isNotBlank(manager.getManagerAdmin())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager")}));
        } else {
            manager.setManagerAdmin("");
            if (StringUtils.isBlank(manager.getManagerNickName())) {
                return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("manager.nickname")}));
            } else if (!StringUtil.checkLength(manager.getManagerNickName() + "", 1, 15)) {
                return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("manager.nickname"), "1", "15"}));
            } else if (StringUtils.isBlank(manager.getManagerPassword())) {
                return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("manager.password")}));
            } else if (!StringUtil.checkLength(manager.getManagerPassword() + "", 6, 30)) {
                return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("manager.password"), "6", "30"}));
            } else if (!manager.getManagerPassword().matches("(?!^(\\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\\w~!@#$%^&*?]{6,30}$")) {
                return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager.password")}));
            } else {
                manager.setManagerPassword(SecureUtil.md5(manager.getManagerPassword()));
                this.managerBiz.save(manager);
                this.managerBiz.updateCache();
                return ResultData.build().success(manager);
            }
        }
    }

    @ApiOperation("批量删除管理员")
    @LogAnn(
            title = "批量删除管理员",
            businessType = BusinessTypeEnum.DELETE
    )
    @PostMapping({"/delete"})
    @ResponseBody
    @RequiresPermissions({"basic:manager:del"})
    public ResultData delete(@RequestBody List<ManagerEntity> managers, HttpServletResponse response, HttpServletRequest request) {
        ManagerEntity manager = BasicUtil.getManager();
        Integer[] ids = new Integer[managers.size()];

        for(int i = 0; i < managers.size(); ++i) {
            ids[i] = Integer.parseInt(((ManagerEntity)managers.get(i)).getId());
        }

        List<ManagerEntity> managerEntities = this.managerBiz.listByIds(Arrays.asList(ids));
        managerEntities = (List)managerEntities.stream().filter((managerEntity) -> {
            return ManagerAdminEnum.SUPER.toString().equals(managerEntity.getManagerAdmin()) || ManagerAdminEnum.SUPERADMIN.toString().equals(managerEntity.getManagerAdmin()) || manager.getId().equals(managerEntity.getId());
        }).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(managerEntities)) {
            this.LOG.error("非法操作删除超管账号或自己账号");
            return ResultData.build().error(this.getResString("fail", new String[]{this.getResString("remove")}));
        } else {
            this.managerBiz.removeByIds(Arrays.asList(ids));
            this.managerBiz.updateCache();
            return ResultData.build().success();
        }
    }

    @ApiOperation("更新管理员信息管理员")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "managerName",
            value = "帐号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerNickName",
            value = "昵称",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerPassword",
            value = "密码",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "roleId",
            value = "角色ID",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "peopleId",
            value = "用户ID",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "更新管理员信息管理员",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"/update"})
    @ResponseBody
    @RequiresPermissions({"basic:manager:update"})
    public ResultData update(@ModelAttribute @ApiIgnore ManagerEntity manager) {
        if (StringUtils.isBlank(manager.getManagerName())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("manager.name")}));
        } else if (!StringUtil.checkLength(manager.getManagerName() + "", 6, 15)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("manager.name"), "6", "15"}));
        } else {
            this.managerBiz.updateCache();
            ManagerEntity _manager = this.managerBiz.getManagerByManagerName(manager.getManagerName());
            if (_manager != null && !_manager.getId().equals(manager.getId())) {
                return ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("manager.name")}));
            } else {
                _manager = (ManagerEntity)this.managerBiz.getById(manager.getId());
                if (_manager == null) {
                    return ResultData.build().error(this.getResString("err.not.exist", new String[]{this.getResString("managerName")}));
                } else if (StringUtils.isNotBlank(manager.getManagerAdmin())) {
                    return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager")}));
                } else if (!manager.getManagerName().matches("^[a-zA-Z0-9_]{6,15}$")) {
                    return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager.name")}));
                } else if (StringUtils.isBlank(manager.getManagerNickName())) {
                    return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("manager.nickname")}));
                } else if (!StringUtil.checkLength(manager.getManagerNickName() + "", 1, 15)) {
                    return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("manager.nickname"), "1", "15"}));
                } else {
                    if (!StringUtils.isBlank(manager.getManagerPassword())) {
                        if (!StringUtil.checkLength(manager.getManagerPassword() + "", 6, 30)) {
                            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("manager.password"), "6", "30"}));
                        }

                        if (!manager.getManagerPassword().matches("(?!^(\\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\\w~!@#$%^&*?]{6,30}$")) {
                            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager.password")}));
                        }

                        manager.setManagerPassword(SecureUtil.md5(manager.getManagerPassword()));
                    } else {
                        manager.setManagerPassword((String)null);
                    }

                    this.managerBiz.updateById(manager);
                    return ResultData.build().success(manager);
                }
            }
        }
    }
}
